package com.Interface.pa;

public class Test3 {
    public static void cut(String str){
        System.out.println(str);
        int index = 1;
        String[] strings = new String[1];
        boolean isflag = true;
        int count = 0;
        while (isflag){
            int i = str.indexOf(" ");
            if (i > 0){
                strings[count] = str.substring(0, i);
                index++;
                strings = tryArray(strings,index);
                str = str.substring(i + 1,str.length());
                count++;
            }else {
                strings[count] = str;
                isflag = false;
            }

        }
        for (String string : strings) {
            System.out.println(string + "\n");
        }
    }
    public static  String[] tryArray(String[] oldArray,int index){
        return new String[index];
    }

    public static void trySplit(String str) {
        int spaceCount = 0;
        //找空格
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == ' ') {
                spaceCount++;
            }
        }

        int[] indexs = new int[spaceCount];
        int index = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == ' ') {
                indexs[index] = i;
                index++;
            }
        }

        for (int i = 0; i <= spaceCount; i++) {
            int start = (i == 0) ? 0 : indexs[i - 1] + 1;
            int end = (i == spaceCount) ? str.length() : indexs[i];
            String substring = str.substring(start, end);
            System.out.println(substring);
        }
    }

    public static void main(String[] args) {
        String str = "dio naso sal sa s csr s ";
//        cut(str);
        trySplit(str);
    }
}
